package cn.big.bigd.ybp.controller;

import java.util.List;

import cn.big.bigd.ybp.util.EmailUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.big.bigd.ybp.model.YbpUser;
import cn.big.bigd.ybp.service.YbpUserService;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author fan
 */

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    YbpUserService userService;

    @RequestMapping("super/home")
    public String superadmin(Model model) {
         return "ybp/superadmin";
    }

    /**
     * 处理管理员审核界面
     *
     * @param model
     * @return
     */
    @RequestMapping("home")
    public String admin(Long show, Model model) {
        show(show, model,0);
        return "ybp/audit";
    }

    private void show(Long show, Model model,int type) {
        List<YbpUser> users;
        if (show != null) {
            if (show == 0) {
                users = userService.findByStatus(2);
            } else if (show == 1) {
                users = userService.findByStatusAndThirteenLunch(2, 0);
            } else if (show == 2) {
                users = userService.findByStatusAndTwelveDinner(2, 0);
            } else if (show == 3) {
                users = userService.findByStatusAndThirteenDinner(2, 0);
            }else if (show ==4) {
                users = userService.findByStatusAndUserTypeOrderByUsernameAsc(2, "PI/Senior Scientist");
            }
            else if (show ==5) {
                users =userService.findByStatusAndUserTypeNotOrderByUsernameAsc(2, "PI/Senior Scientist");
            }

            else {
                users = userService.findAll();
            }

        } else {
            if(type==0){
                users = userService.findAll();
            }else if(type==1){
                users = userService.findByStatusOrderByUsernameAsc(2);
            }   else if (type ==2) {
                users =userService.findByStatusAndUserTypeAndIssueNotNullOrderByUsernameAsc(2, "PI/Senior Scientist");
            }
            else {
                users = userService.findAll();
            }

        }

        model.addAttribute("users", users);
        // 统计审核通过的参会人数
        model.addAttribute("attendNumber", userService.countByStatus(2));
        model.addAttribute("thirteenLunch", userService.countByThirteenLunch(0));
        model.addAttribute("twelveDinner", userService.countByTwelveDinner(0));
        model.addAttribute("thirteenDinner", userService.countByThirteenDinner(0));
        model.addAttribute("total", users.size());
    }


    @RequestMapping("print")
    public String print(Long show, Model model) {
        show(show, model,1);
        return "ybp/print";
    }

    @RequestMapping("disprint")
    public String disprint(Long show, Model model) {
        show(show, model,2);
        return "ybp/disprint";
    }

    @RequestMapping(value = "/ajaxaudit", method = RequestMethod.POST)
    @ResponseBody
    public Integer ajaxaudit(HttpServletRequest request, Model model) {
        Long id = Long.valueOf(request.getParameter("id"));
        YbpUser user = userService.findById(id);
        if (user.getStatus() == 1) {
            user.setStatus(2);
        } else if (user.getStatus() == 2) {
            user.setStatus(1);
        }
        userService.save(user);
        return user.getStatus();

    }

    /**
     * 审核用户信息
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/audit")
    public String audit(@RequestParam("id") Long id, @RequestParam("status") Integer status, Model model) {

        if (status == 1) {
            userService.updateStatus(2, id);
//            YbpUser user = userService.findById(id);
//            EmailUtils.sendHtmlMail("YBP用户审核成功", "恭喜您, 您的会议注册已经审核通过, 请准时参会, 会议更多信息请您查看会议主页 <a href='http://bigd.big.ac.cn/conference/ybp'", user.getEmail());

        } else if (status == 2) {
            userService.updateStatus(1, id);
        }

        List<YbpUser> users = userService.findAll();
        model.addAttribute("users", users);
        model.addAttribute("attendNumber", userService.countByStatus(2));
        model.addAttribute("thirteenLunch", userService.countByThirteenLunch(0));
        model.addAttribute("twelveDinner", userService.countByTwelveDinner(0));
        model.addAttribute("thirteenDinner", userService.countByThirteenDinner(0));
        model.addAttribute("total", users.size());
        return "ybp/audit";

    }


    /**
     * 管理员删除用户信息
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/drop")
    public String drop(@RequestParam("id") Long id, Model model) {

        userService.deleteById(id);
        List<YbpUser> users = userService.findAll();
        model.addAttribute("users", users);
        model.addAttribute("attendNumber", userService.countByStatus(2));
        model.addAttribute("thirteenLunch", userService.countByThirteenLunch(0));
        model.addAttribute("twelveDinner", userService.countByTwelveDinner(0));
        model.addAttribute("thirteenDinner", userService.countByThirteenDinner(0));
        return "ybp/audit";

    }

    @RequestMapping("q")
    public String query(@RequestParam("status") String status, @RequestParam("twelveDinner") String twelveDinner, @RequestParam("thirteenDinner") String thirteenDinner, @RequestParam("thirteenLunch") String thirteenLunch, @RequestParam("userType") String userType, @RequestParam("isInvited") String isInvited, ModelMap modelMap) {
        List<YbpUser> list = userService.multiQuery(status, twelveDinner, thirteenDinner, thirteenLunch, isInvited, userType);
        modelMap.put("status", status);
        modelMap.put("twelveDinner", twelveDinner);
        modelMap.put("thirteenDinner", thirteenDinner);
        modelMap.put("thirteenLunch", thirteenLunch);
        modelMap.put("userType", userType);
        modelMap.put("isInvited", isInvited);
        modelMap.addAttribute("attendNumber", userService.countByStatus(2));
        modelMap.addAttribute("thirteenLunch", userService.countByThirteenLunch(0));
        modelMap.addAttribute("twelveDinner", userService.countByTwelveDinner(0));
        modelMap.addAttribute("thirteenDinner", userService.countByThirteenDinner(0));
        modelMap.put("total", list.size());
        modelMap.put("users", list);
        return "ybp/audit";
    }


}
